﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace AndroidEditor.WForm
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public partial class AndroidPlatForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private int aVersion = -1;
        /// <summary>
        /// Initializes a new instance of the <see cref="AndroidPlatForm"/> class.
        /// </summary>
        /// <remarks></remarks>
        public AndroidPlatForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AndroidPlatForm"/> class.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        public AndroidPlatForm(int version)
        {
            aVersion = version;
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Gets the select version.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public int GetSelectVersion()
        {
            return aVersion;
        }

        /// <summary>
        /// Handles the Load event of the AndroidPlatForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void AndroidPlatForm_Load(object sender, EventArgs e)
        {
            string ndk = GlobleSetting.Instance().GetNDK();
            DirectoryInfo directory = new DirectoryInfo(ndk + @"\platforms");
            DirectoryInfo[] alldir = directory.GetDirectories("android-*");
            int selectindex =  -1;
            foreach (DirectoryInfo item in alldir)
            {
              
             int   selectindexTmp = comboBox1.Items.Add(item.Name);
              
                if (aVersion == -1)
                {
                    if (selectindex == -1)
                    {
                        selectindex = selectindexTmp;
                        comboBox1.SelectedIndex = selectindex;
                    }
                }
                else
                {
                    Regex regex = new Regex(@"\d+");
                    Match match = regex.Match(item.Name);
                    if (aVersion.ToString() == match.Value)
                    {
                        comboBox1.SelectedIndex = selectindexTmp;
                    }
                }
            }

        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button1_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"\d+");
            Match match = regex.Match(comboBox1.Items[comboBox1.SelectedIndex].ToString());
            aVersion =  Convert.ToInt16(match.Value);
            DialogResult = DialogResult.OK;
        }
    }
}
